//-------------------------------------------------------------------------
// File: Util.cpp
//
// Descr.: - todo -
//
// Author: Stefan Leihkauf
// Date:   23.03.2014
//-------------------------------------------------------------------------
#include "cb_pch.h"
#include "Util.h"

#include <locale>

namespace cb { namespace common { namespace util {

//-------------------------------------------------------------------------
// Split string delimited by given string delimiter
//-------------------------------------------------------------------------

StringList StringSplit(const StdString& str, const StdString& delim)
{
    StringList strList;

    if (!str.empty()) {
        std::size_t posSubStart = 0;
        std::size_t posSubEnd = 0;

        while ((posSubEnd = str.find(delim, posSubStart)) != StdString::npos) {
            StdString strSub = str.substr(posSubStart, posSubEnd - posSubStart);
            strList.push_back(strSub);
            
            posSubStart = (posSubEnd + delim.length());
        }

        if ((posSubStart > 0) && (posSubStart < str.length())) {
            strList.push_back(str.substr(posSubStart, str.length() - posSubStart));
        }
    }

    return strList;
}

//-------------------------------------------------------------------------
// Check if the given string can be converted to an integral number.
//-------------------------------------------------------------------------

bool isIntegralNumber(const StdString& s)
{
    std::locale loc;
    StdString::const_iterator it = s.begin();
    while (it != s.end() && std::isdigit(*it, loc))
        ++it;

    return ((!s.empty()) && (it == s.end()));
}

}}} // namespace cb::common::util